/*
 * Copyright (c) 2011, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "./GLPrivate.h"

#include "alkes/opengl/GLMatrixStack.h"
#include "alkes/math/Math.h"

namespace alkes {

#if !defined(ALKES_GLES2)

#if defined(ALKES_GLES)
static void gluPerspective(float fovy, float aspect, float znear, float zfar);
static void gluLookAt(
    float eyeX, float eyeY, float eyeZ,
    float centerX, float centerY, float centerZ,
    float upX, float upY, float upZ);
#endif

GLMatrixStack::GLMatrixStack()
{
}

GLMatrixStack::~GLMatrixStack()
{
}

void GLMatrixStack::setMode(MatrixMode mode)
{
    AL_OGL_INVOKE(glMatrixMode(mode == AL_MM_PROJECTION ? GL_PROJECTION : GL_MODELVIEW));
}

void GLMatrixStack::pushMatrix()
{
    AL_OGL_INVOKE(glPushMatrix());
}

void GLMatrixStack::popMatrix()
{
    AL_OGL_INVOKE(glPopMatrix());
}

void GLMatrixStack::identity()
{
    AL_OGL_INVOKE(glLoadIdentity());
}

void GLMatrixStack::multiply(const Matrix& m)
{
    glMultMatrixf(m);
}

void GLMatrixStack::perspective(float fovy, float aspect, float znear, float zfar)
{
    gluPerspective(fovy, aspect, znear, zfar);
}

void GLMatrixStack::frustum(float left, float right, float bottom, float top, float znear, float zfar)
{
    glFrustum(left, right, bottom, top, znear, zfar);
}

void GLMatrixStack::ortho(float left, float right, float bottom, float top, float znear, float zfar)
{
    glOrtho(left, right, bottom, top, znear, zfar);
}

void GLMatrixStack::lookAt(const Vector3D& eye, const Vector3D& at, const Vector3D& up)
{
    gluLookAt(eye.x, eye.y, eye.z, at.x, at.y, at.z, up.x, up.y, up.z);
}

void GLMatrixStack::translate(float x, float y, float z)
{
    AL_OGL_INVOKE(glTranslatef(x, y, z));
}

void GLMatrixStack::scale(float xscale, float yscale, float zscale)
{
    AL_OGL_INVOKE(glScalef(xscale, yscale, zscale));
}

void GLMatrixStack::rotate(float angle, float x, float y, float z)
{
    AL_OGL_INVOKE(glRotatef(angle, x, y, z));
}

static void print_matrix(const char* sig, const Matrix* matrix)
{
    LOGD("==== GLMatrixStack@%s ====", sig);
    LOGD("|%2.6f %2.6f %2.6f %2.6f|", matrix->m_[0][0], matrix->m_[0][1], matrix->m_[0][2], matrix->m_[0][3]);
    LOGD("|%2.6f %2.6f %2.6f %2.6f|", matrix->m_[1][0], matrix->m_[1][1], matrix->m_[1][2], matrix->m_[1][3]);
    LOGD("|%2.6f %2.6f %2.6f %2.6f|", matrix->m_[2][0], matrix->m_[2][1], matrix->m_[2][2], matrix->m_[2][3]);
    LOGD("|%2.6f %2.6f %2.6f %2.6f|", matrix->m_[3][0], matrix->m_[3][1], matrix->m_[3][2], matrix->m_[3][3]);
}

void GLMatrixStack::printMatrix()
{
#if 1
    GLfloat m[16];
    glGetFloatv(GL_PROJECTION_MATRIX, m);
    print_matrix("projection", (const Matrix*)m);

    glGetFloatv(GL_MODELVIEW_MATRIX, m);
    print_matrix("modelview", (const Matrix*)m);
//    print_matrix("calculated", calc_matrix_);
#endif
}

#if defined(ALKES_GLES)

static void gluPerspective(float fovy, float aspect, float znear, float zfar)
{
    float h = tanf(fovy / 360.0f * PI) * znear;
    float w = h * aspect;

    glFrustumf(-w, w, -h, h, znear, zfar);
}

static void gluLookAt(
    float eyeX, float eyeY, float eyeZ,
    float centerX, float centerY, float centerZ,
    float upX, float upY, float upZ)
{
    Matrix tmp;

    Matrix::lookAt(
        tmp,
        UVector3D(eyeX, eyeY, eyeZ),
        UVector3D(centerX, centerY, centerZ),
        UVector3D(upX, upY, upZ));

    glMultMatrixf(&tmp.m_[0][0]);
}

#endif

#endif

}
